#!/bin/bash
# 安装python依赖
sources=(
    "https://pypi.org/simple/"  # Python 官方源
    "https://pypi.tuna.tsinghua.edu.cn/simple/"  # 清华大学镜像源
    "https://mirrors.aliyun.com/pypi/simple/"  # 阿里云镜像源
    "https://pypi.hustunique.com/simple/"  # 华中科技大学镜像源
)

fastest_source=""
min_time=999999999

for source in "${sources[@]}"; do
    echo "测试pip源: $source"
    start_time=$(date +%s%N)
    wget -q --spider "$source" 2>/dev/null
    end_time=$(date +%s%N)
    elapsed_time=$(( (end_time - start_time) / 1000000 ))
    if [ $? -eq 0 ]; then
        echo "与 $source 的连接成功. 耗时: $elapsed_time ms"
        if [ $elapsed_time -lt $min_time ]; then
            min_time=$elapsed_time
            fastest_source=$source
        fi
    else
        echo "连接到 $source 失败."
    fi
    echo "-------------------"
done

if [ -n "$fastest_source" ]; then
    echo "******************安装python依赖中******************"
    python3 -m pip install -i $fastest_source -r src/requirements.txt
    if [ $? -ne 0 ]; then
        echo "python依赖安装失败或未正确安装。"
        exit 1
    else
        echo "python依赖安装成功。"
    fi
else
    echo "未能找到python源,依赖安装失败。"
    exit 1
fi
# 安装ros依赖
sudo apt install ros-noetic-tf2-web-republisher 
if [ $? -ne 0 ]; then
    echo "tf2-web-republisher安装失败或未正确安装。"
    exit 1
else
    echo "tf2-web-republisher安装成功。"
fi
sudo apt install ros-noetic-rosbridge-server
if [ $? -ne 0 ]; then
    echo "rosbridge-server安装失败或未正确安装。"
    exit 1
else
    echo "rosbridge-server安装成功。"
fi